/*
Web: http://www.jsForms.com
Copyright (C) 2008  Igor Demyanov (mailto:Igor.Demyanov@gmail.com)
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
jsForms.Components.ComponentEditor = {
	//Properties:
	//EditComponentType - current type
	//ComponentDesign - current design component
	//ComponentReal - current real component
	Init: function(){
		this.C.WinCSS.bind('click',this,this.ActiveWindow).toggleClass('CEWinHide');
		this.C.WinPE.bind('click',this,this.ActiveWindow).toggleClass('CEWinHide');
		this.C.WinDOM.bind('click',this,this.ActiveWindow).toggleClass('CEWinHide');
		this.C.WinInfo.bind('click',this,this.ActiveWindow).toggleClass('CEWinHide');
		this.C.WinColorPicker.bind('click',this,this.ActiveWindow).toggleClass('CEWinHide');
		
		this.C.WinTools.bind('click',this,this.ActiveWindow);
		this.C.HighlightElement.SetOffsetObject(this.C.tabDesign);
		return jsForms.Components.Component.Init.call(this);
	},
	HE_onResize:function()
	{
		alert('HE_onResize');
	},
	HE_onMove:function()
	{
		alert('HE_onMove');
	},
	Windows_onClose:function(win)
	{
		win.toggleClass('CEWinHide');
	},
	ActiveWindow:function(e)
	{
		if (!e.data)
		{
			return;
		}
		//debugger;
		e.data.OnTopWindow(jsForms.GetObject(this));
	},
	bApply_onClick:function()
	{
		if (!this.P.ComponentDesign) {
			return;
		}
		jsForms.Templates[this.P.EditComponentType] = jsForms.DesignMode.CreateTemplate(this.P.ComponentDesign);
		this.Update();
	},
	PETool_onChange:function()
	{
		this.C.DOMTool.UpdateNode();
	},
	CSSTool_onChange:function()
	{
		this.C.DOMTool.UpdateNode();
		this.C.HighlightElement.Update();
	},
	CSSTool_onInsert:function()
	{
		this.C.background._SetEvent('onShow',this,this.InsertCSSDialog_Show);
		this.C.background.Show();
	},
	InsertCSSDialog_Show:function()
	{
		this.C.WinInsertCSS.show();
	},
	InsertCSSDialog_Cancel:function()
	{
		this.C.WinInsertCSS.hide();
		this.C.background.Hide();
	},
	InsertCSSDialog_Ok:function(result)
	{
		this.C.WinInsertCSS.hide();
		this.C.background.Hide();
		if (result)
		{
			this.C.CSSTool.InsertCssSelector(result.className);
		}
	},
	DOMTool_onInsert:function()
	{
		this.C.background._SetEvent('onShow',this,this.InsertDialog_Show);
		this.C.background.Show();
	},
	InsertDialog_Show:function()
	{
		this.C.InsertCompDialog.FillComponentList();
		this.C.WinInsert.show();
	},
	InsertDialog_Cancel:function()
	{
		this.C.WinInsert.hide();
		this.C.background.Hide();
	},
	InsertDialog_Ok:function(result)
	{
		/*
		result:
			isTag
				Tag
			ComponentName
		*/
		this.C.WinInsert.hide();
		this.C.background.Hide();
		this.C.DOMTool.InsertNode(result);
	},
	OnTopWindow:function(win)
	{
		if (this.P.ActiveWindow != win)
		{
			this.P.ActiveWindow = win;
		}else
		{
			return;
		}
		if (this.P.ActiveWindowIndex == undefined)
		{
			this.P.ActiveWindowIndex = 0;
		}
		else
		{
			this.P.ActiveWindowIndex++;
		}
		win.css('zIndex',this.P.ActiveWindowIndex);
	},
	ToggleCSSWindow:function()
	{
		this.C.WinCSS.toggleClass('CEWinHide');
	},
	TogglePEWindow:function()
	{
		this.C.WinPE.toggleClass('CEWinHide');
	},
	ToggleDOMWindow:function()
	{
		this.C.WinDOM.toggleClass('CEWinHide');
	},
	ToggleInfoWindow:function()
	{
		this.C.WinInfo.toggleClass('CEWinHide');
	},
	ToggleColorPicker:function()
	{
		this.C.WinColorPicker.toggleClass('CEWinHide');
	},
	SetComponentName: function(compType){
		// --------------------------------
		// DEBUG
		//return;
		// --------------------------------
		if (compType == undefined) {
			var sE = '';
			console.error(sE);
			alert(sE);
			throw sE;
		}
		this.P.EditComponentType = compType;
		this.C.PETool.SetEditComponentType(compType);
		this.C.InfoTool.SetEditComponentType(compType);
		this.Update();
	},
	Update: function(){
		// --------------------------------
		// DEBUG
		//return;
		// --------------------------------
		this.C.Screen[0].innerHTML = '';
		var compType = this.P.EditComponentType;
		var options = {
			GenName:true,
			Design:true
		};
		this.P.ComponentReal = jsForms.DesignMode.CreateComponent(compType,options);
		this.P.ComponentDesign =options.DesignRoot;
		//this.P.ComponentDesignList = options.DesignArray;
		this.P.ComponentReal.addClass('EditComponent').hide().appendTo(this.C.Screen).Init().show();
		this.UpdateDOM();
		this.C.InfoTool.Update();
	},
	outputTabSelect:function()
	{
		this.C.OutputFiles.GenerateOutput(this.P.ComponentDesign,this.P.EditComponentType);
	},
	UpdateDOM:function()
	{
		this.C.DOMTool.SetComponent(this.P.ComponentDesign,this.P.EditComponentType);
	},
	UpdateCSS:function(node)
	{
		this.C.CSSTool.SetObject(node);
	},
	UpdatePE:function(node)
	{
		this.C.PETool.SetObject(node);
	},

	UpdateHighlight: function(node){

		this.C.HighlightElement.SetNode(node);		
	},
	DOMTool_onSelect:function(item)
	{
		var node;
		if (item) {
			node = item.node;
		}
		this.UpdatePE(node);
		this.UpdateCSS(node);
		this.UpdateHighlight(node);
	},
	sourceTabSelect: function(){
		this.C.templateSourceEditor.Show();
		if (!this.P.ComponentDesign) {
			this.C.templateSourceEditor.SetSource('');
			return;
		}
		var templateNew = jsForms.DesignMode.CreateTemplate(this.P.ComponentDesign,true);
		this.C.templateSourceEditor.SetSource(templateNew);
	},
	sourceTabDeselect: function(eventArg){
		if (!this.P.ComponentDesign) {
			return;
		}
		var template = this.C.templateSourceEditor.GetSource();
		template = template.replace(/[\n\t]*/g, '');
		jsForms.Templates[this.P.EditComponentType] = template
		try {
			this.Update();
		}catch(e)
		{
			if (eventArg)
			{
				eventArg.isCancel = true;
			}
			if (e) {
				if (e.message) {
					alert(e.message);
				}else
				{
					alert(e.toString());
				}
			}
			else {
				alert('Error');
			}
			return;
		}
		this.C.templateSourceEditor.Hide();
		
	}
};
